import { proxy } from 'valtio'
import { get } from '@/request.ts'

interface searchOptions {
    // 品牌
    brand: string
    // 分区
    partition: string
    // 店铺
    shop: string
    // 来源
    source: string
    // 近日
    recently: string
    // 日期
    date: string
}

interface resultOptions {
    // 实收金额
    PaidInAmount: number
    // 实付总额
    TotalPaid: number
    // 有效订单
    validOrderNumber: number
    // 有效订单占比
    validOrderPercent: number
    // 销售订单数
    SalesOrderNumber: number
    // 人均消费金额
    PerCapitaConsumptionAmount: number
    // 新客
    NewCustomer: number
    // 老客
    patron: number
    meituanNumber: number
    elmNumber: number
    meituanNewCustomers: number
    meituanOldCustomers: number
    elmNewCustomers: number
    elmOldCustomers: number
    PricePrecent: number
}

export const yun = proxy({
    // 搜索对象
    searchOptions: {
        brand: '',
        partition: '',
        shop: '',
        source: '',
        recently: '',
        date: ''
    } as searchOptions,
    // 结果对象
    resultOptions: {
        PaidInAmount: 0,
        TotalPaid: 0,
        SalesOrderNumber: 0,
        validOrderNumber: 0,
        validOrderPercent: 0,
        PerCapitaConsumptionAmount: 0,
        NewCustomer: 0,
        patron: 0,
        meituanNumber: 0,
        elmNumber: 0,
        meituanNewCustomers: 0,
        meituanOldCustomers: 0,
        elmNewCustomers: 0,
        elmOldCustomers: 0,
        PricePrecent: 0
    } as resultOptions,
    // 新增客户数据
    AddCustomer: {
        tooltip: {},
        // xAxis: {
        //     type: 'category',
        //     // boundaryGap: false,
        //     data: []
        // },
        xAxis: [
            {
                type: 'category',
                data: [],
                position: 'bottom'
            },
            {
                type: 'category',
                data: [],
                position: 'bottom'
            }
        ],
        yAxis: {
            type: 'value'
        },
        series: [
            {
                name: '人数',
                type: 'line',
                smooth: true,
                stack: '总量',
                data: []
            },
            {
                name: '人数',
                type: 'line',
                smooth: true,
                stack: '总量',
                data: []
            }
        ]
    },
    // 获取搜索选项
    getSearch: async (filterObj = {}) => {
        const {
            SelectOptions,
            AddCustomer,
            result,
        }: {
            SelectOptions: any
            AddCustomer: any
            result: any
            filterData: any
        } = await get('yun/SearchOptions', filterObj)
        yun.brand = SelectOptions.brand
        yun.partition = SelectOptions.partition
        yun.shop = SelectOptions.shop
        yun.source = SelectOptions.source
        yun.resultOptions = result
        yun.AddCustomer.xAxis[0].data = AddCustomer.date
        yun.AddCustomer.series[0].data = AddCustomer.data
        yun.AddCustomer.series[1].data = AddCustomer.data
    },
    // 品牌
    brand: [{ label: '所有品牌', value: '所有品牌' }],
    // 分区
    partition: [{ label: '所有分区', value: '所有分区' }],
    // 店铺
    shop: [{ label: '所有店铺', value: '所有店铺' }],
    // 来源
    source: [{ label: '所有来源', value: '所有来源' }],
    // 筛选
    filter: async (data: any) => {
        yun.searchOptions = data
        await yun.getSearch(yun.searchOptions)
    }
})
